import tkinter
import tkinter.messagebox
import numpy
import math
def quadratic1(a,b,c):
    x1 = (-b + math.sqrt((b * b) - (4 * a * c))) / (2 * a)
    return x1
def quadratic2(a,b,c):
    x2 = (-b - math.sqrt((b * b) - (4 * a * c))) / (2 * a)
    return x2
# login command function
def login():
    #identity_var = identity.get()
    try:
        clv = [float(val) for val in clVal.get().split(',')]
        mbv = [float(val) for val in mbVal.get().split(',')]
        nhv = round(float(nhVal.get()))
        # python Sequence unpacking
        #cc = [20.4,36.5,53.8,72.2,89.7]
        #bb = [23.2,46.2,69.8,92.0,115.0]
           
        #xx = [(x for x in clv,y for y in mbv)]
        xsv = numpy.polyfit(clv,mbv,nhv)
        val4.set(",".join(str(round(val,4)) for val in xsv))
        #tkinter.messagebox.showinfo(title='Landing', message='Identity error')
    except:
        val4.set("请检查输入的值是否正确，逗号是分隔符！")
    #wcs = tkinter.Tk()
    #canvas.create_image(200, 200, image=img)
    cx = 5
    cy = 10

    xmax = round(max(abs(max(clv)),abs(min(clv))))
    ymax = round(max(abs(max(mbv)),abs(min(clv))))

    xx = []
    for i in range(len(clv)):
        xx.append(((clv[i]/xmax+1)*200+cx,cy+100*(1-mbv[i]/ymax)))

    '''
    for i in range(len(mbv)):
        jj = len(xsv)-1
        mbvv = 0
        for j in range(jj):
            mbvv += ((mbv[i])**(jj-j))*xsv[j]
            print(j,xsv[j],mbvv,jj-j,jj,mbv[i])
        mbvv += xsv[jj]
        print(mbvv)
        yy.append(((mbvv/xmax+1)*200+cx,cy+100*(1-mbv[i]/ymax)))
    print(yy)
    '''
    canvas.delete("all")
    canvas.create_line(cx+0, cy+100, cx+399, cy+100)  # x-axis
    canvas.create_line(cx+200, cy+0, cx+200, cy+199)  # y-axis

    canvas.create_text(cx+190, cy+110, text="0")
    canvas.create_text(cx+420, cy+110, text="测量值")
    canvas.create_text(cx+165, cy, text="目标值")
    xm = xmax//5
    ym = ymax//5    
    for i in range(1,5):
        x = i*40
        canvas.create_line(cx+200+x, cy+98, cx+200+x, cy+103)  # x-axis
        canvas.create_text(cx+200+x, cy+110, text=str(i*xm))
        canvas.create_line(cx+200-x, cy+98, cx+200-x, cy+103)  # x-axis
        canvas.create_text(cx+200-x, cy+110, text=str(-i*xm))

    for i in range(1,5):
        x = i*20
        canvas.create_line(cx+198, cy+100-x, cx+203, cy+100-x)  # x-axis
        canvas.create_text(cx+185, cy+100-x, text=str(i*ym))
        canvas.create_line(cx+198, cy+100+x, cx+203, cy+100+x)  # x-axis
        canvas.create_text(cx+185, cy+100+x, text=str(-i*ym))
    for x in xx:
        canvas.create_oval(x[0]-2, x[1]-2, x[0]+2, x[1]+2,fill="red")
    #canvas.create_line(xx, dash=(2,2), fill="red")
    ii = len(xsv)

    if (ii == 2):
        yy = []
        for i in range(len(mbv)):
            yy.append(((((mbv[i]-xsv[1])/xsv[0])/xmax+1)*200+cx,cy+100*(1-mbv[i]/ymax)))
        canvas.create_line(yy)
    elif (ii==3):
        yy = []
        for i in range(len(mbv)):
            yy.append(((quadratic1(xsv[0],xsv[1],xsv[2])/xmax+1)*200+cx,cy+100*(1-mbv[i]/ymax)))
        canvas.create_line(yy)
        yy = []
        for i in range(len(mbv)):
            yy.append(((quadratic2(xsv[0],xsv[1],xsv[2])/xmax+1)*200+cx,cy+100*(1-mbv[i]/ymax)))
        canvas.create_line(yy)
    
    canvas.pack()
    fmr.pack()

if __name__ == '__main__':
    # Window Configuration
    windows = tkinter.Tk()
    windows.attributes("-alpha", 0.95)
    windows.title('辽宁拓新电力电子有限公司')
    windows.geometry('1000x500')
    fmt = tkinter.Frame(windows,height=100,width=1000)
    fml = tkinter.Frame(windows,height=400,width=500)
    fmr = tkinter.Frame(windows,height=400,width=500)
    canvas = tkinter.Canvas(fmr, height=300, width=500)
    val1 = tkinter.StringVar()
    val2 = tkinter.StringVar()
    val3 = tkinter.StringVar()
    val4 = tkinter.StringVar()
    # Label
    tip1 = tkinter.Label(fmt, text='                       欢迎使用   Numpy .........', font=('Arial', 25)) \
        .place(x=100, y=50)
    tip2 = tkinter.Label(fml, text='测量值  :', font=('Arial', 15)).place(x=50, y=50)
    tip3 = tkinter.Label(fml, text='目标值  :', font=('Arial', 15)).place(x=50, y=110)
    tip4 = tkinter.Label(fml, text='拟合度  :', font=('Arial', 15)).place(x=50, y=170)
    tip5 = tkinter.Label(fml, text='系数值  :', font=('Arial', 15)).place(x=50, y=230)
    # Entry
    clVal = tkinter.Entry(fml, font=('Arial', 14),textvariable =val1)
    clVal.place(x=150, y=50, width=300)
    mbVal = tkinter.Entry(fml, font=('Arial', 14),textvariable =val2)
    mbVal.place(x=150, y=110, width=300)

    nhVal = tkinter.Entry(fml, font=('Arial', 14),textvariable =val3)
    nhVal.place(x=150, y=170, width=300)

    xsVal = tkinter.Entry(fml, font=('Arial', 14),textvariable =val4)
    xsVal.place(x=150, y=230, width=300)
    val1.set("20.4,36.5,53.8,72.2,89.7")
    val2.set("23.2,46.2,69.8,92.0,115.0")
    val3.set("1")
    val4.set("计算结果...")
    fmt.pack()
    fml.pack(side="left")
    fmr.pack(side="right")
    # login Button
    loginButton = tkinter.Button(fml, text='开始计算', font=('Arial', 20), command=login).place(x=200, y=300)
    # windows loop refresh
    windows.mainloop()
